<?php

include_once('../service/bannerService.php');
include_once('../util/image/SimpleImage.php');

function addBanner($fileName) {
    return insertBanner($fileName);
}

function getImagesByID($imageID) {
    return loadImagesByID($imageID);
}

function updateBanner($imgID, $fileName) {
    return editBanner($imgID, $fileName);
}

function findExts($filename) {
    $filename = strtolower($filename);
    $exts = split("[/\\.]", $filename);
    $n = count($exts) - 1;
    $exts = $exts[$n];
    return '.' . $exts;
}

function deletePhysicalImage($filePath) {
    if (file_exists($filePath)) {
        unlink($filePath);
    }
}

function uploadImageServive($image, $name, $serviceId){
	$imagePath = "../images/4s/".$serviceId."/";
	$newName = $name.findExts($image["name"]);
	
	if (!isset($image['name']) || $image['name'] == '') {
        return '';
	} else {
		if ((($image["type"] == "image/gif")
                || ($image["type"] == "image/jpg")
                || ($image["type"] == "image/jpeg")
                || ($image["type"] == "image/pjpeg")
                || ($image["type"] == "image/png"))) {

            move_uploaded_file($image["tmp_name"], $imagePath . $newName);
        } else {
            return '';
        }
	}
	return $newName;
}

function uploadBanner($image, $suffix, $oldID) {
	$imagePath = "../images/banner/";
    $newName = $suffix . findExts($image['name']);

    if (!isset($image['name']) || $image['name'] == '') {
        return '';
    } else {
        if ($oldID == '') {
            addBanner($newName);
        } else {
            $imgRow = findBannerId($oldID);
            deletePhysicalImage($imagePath . $imgRow['BANNER_PATH']);
            updateBanner($oldID, $newName);
        }

        if ((($image["type"] == "image/gif")
                || ($image["type"] == "image/jpg")
                || ($image["type"] == "image/jpeg")
                || ($image["type"] == "image/pjpeg")
                || ($image["type"] == "image/png"))) {

            move_uploaded_file($image["tmp_name"], $imagePath . $newName);
        } else {
            return '';
        }
    }
    return $newName;
}
?>